<?php
/**
 * Created by PhpStorm.
 * User: tony
 * Date: 2018/6/4
 * Time: 11:20
 */

namespace app\modules\v1\controllers;

use app\models\ProjectChild;
use app\config\Status;
use app\models\User;
use app\modules\v1\controllers\parent\ParentController;
use yii\data\ActiveDataProvider;

class ProjectChildController extends ParentController
{
    public function actionIndex(){
        $params = \Yii::$app->request->getQueryParams();
        $query = ProjectChild::find()->with(['childFlow' => function($res) {
            $res->andWhere(['valid' => 1])->orderBy('flow_id ASC');
        }]);

        if (isset($params['project_id'])) {
            $query->andWhere(['project_id'=>$params['project_id']]);
        }

        $query->andWhere(['valid'=>1]);
        $pageSize = 100;
        if(isset($params['pageSize'])){
            $pageSize = (int)$params['pageSize'];
        }
        return new ActiveDataProvider([
            // 使用with方法实现贪加载
            'query' => $query,
            // 分页信息
            'pagination' => [
                'pageSize' => $pageSize,
            ],
            // 排序信息
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                ]
            ],
        ]);
    }

    /**
     * @throws \yii\base\InvalidConfigException
     */
    public function actionUpdate(){
        $data = \Yii::$app->request->getBodyParams();
        $model = ProjectChild::findOne((int)$data['id']);
        $model->load($data, '');
        $model->input_user_id = \Yii::$app->user->id;
        $model->updated_at = time();
        if($model->save()){
            return [
                'bCode' => Status::SUCCESS,
                'bData' => $model
            ];
        }
        return [
            'bCode' => Status::FAIL,
            'bData' => $model->errors
        ];

    }

    public function actionTask() {
        $params = \Yii::$app->request->getQueryParams();
        $id   = \Yii::$app->user->id; // 根据token获取到的id
        $user = User::findOne($id);
        $query = ProjectChild::find()->where(['planner' => $user['name']])->with(['childFlow' => function($res) {
            $res->andWhere(['hj_child_flow.valid' => 1]);
        }]);
        $query->andWhere(['{{%project_child}}.valid'=>1]);
        $pageSize = 10;
        if(isset($params['pageSize'])){
            $pageSize = (int)$params['pageSize'];
        }
        return new ActiveDataProvider([
            // 使用with方法实现贪加载
            'query' => $query,
            // 分页信息
            'pagination' => [
                'pageSize' => $pageSize,
            ],
            // 排序信息
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                ]
            ],
        ]);
    }

    public function actionView() {
        $params = \Yii::$app->request->getQueryParams();
        $query = ProjectChild::find()->where(['id' => $params['id']])->with(['childFlow' => function($res) {
            $res->andWhere(['valid' => 1]);
        }])->one();
        return $query;
    }

    /**
     * @throws \yii\base\InvalidConfigException
     */
    public function actionCreate(){
        $data = \Yii::$app->request->getBodyParams();
        if (isset($data['plan_started_at'])) {
            $data['plan_started_at'] = $data['plan_started_at']/1000;
        }
        if (isset($data['plan_completed_at'])) {
            $data['plan_completed_at'] = $data['plan_completed_at']/1000;
        }
        $model = new ProjectChild();
        $model->load($data, '');
        $model->input_user_id = \Yii::$app->user->id;
        $model->created_at = $model->updated_at = time();
        if($model->save()){
            return [
                'bCode' => Status::SUCCESS,
                'bData' => $model
            ];
        }
        return [
            'bCode' => Status::FAIL,
            'bData' => $model->errors
        ];
    }
}




